-- 技能“谨有”：在回合开始、结束阶段开始、或受到伤害后触发；
-- 仅效果：从诸葛亮（trueName 以 zhugeliang 结尾）的技能中随机获得一个你未拥有的技能，然后摸一张牌。
local yingyou = fk.CreateSkill {
    name = "jinyou",
  }
  
  Fk:loadTranslationTable{
    ["jinyou"] = "尽有",
    [":jinyou"] = "你的回合开始时或当你受到伤害后，你可以减1点体力上限，随机获得一个诸葛亮的技能，然后摸一张牌。",
  }
  
  -- 工具库（用于派生卡牌、过滤等）
  local U = require "packages/utility/utility"
  
  -- 收集可用的“诸葛亮”武将的所有技能（去重且排除已拥有的）
  local function GetZhugeliangSkills(player)
    local room = player.room
    local mappers = room:getBanner("huyi_wuhushangjiang")
    if mappers == nil then
      local skills = {}
      local generals = {}
      local SGmapper = {}
      for name, general in pairs(Fk.generals) do
        if Fk:canUseGeneral(name) and general.trueName:endsWith("zhugeliang") then
          table.insert(generals, general)
        end
      end
      if #generals == 0 then return {} end
      for _, general in ipairs(generals) do
        local list = general:getSkillNameList(true)
        for _, skill in ipairs(list) do
          table.insert(skills, skill)
          SGmapper[skill] = general.name
        end
      end
      mappers = { skills, SGmapper }
      room:setBanner("kaifaceshi_jinyou_zhgl", mappers)
    end
    return table.filter(mappers[1], function(s) return not player:hasSkill(s, true) end)
  end
  
  local yingyouSpec = {
    -- 触发条件：事件目标为自己，且你拥有该技能
    ---@class TrigFunc
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(yingyou.name)
    end,
    -- 选择成本：是否发动，若选择发动将减1点体力上限
    ---@class TrigFunc
    on_cost = function(self, event, target, player, data)
      return player.room:askToSkillInvoke(player, { skill_name = yingyou.name })
    end,
    -- 结算：先减1点体力上限，再获得诸葛亮技能并摸一张牌
    ---@class TrigFunc
    on_use = function(self, event, target, player, data)
      ---@type string
      local skillName = yingyou.name
      local room = player.room
  
      room:changeMaxHp(player, -1)
      -- 随机从“诸葛亮”技能中挑选一个未拥有的，授予给自己
      local skills = table.random(GetZhugeliangSkills(player), 1)
      if #skills == 0 then return end
      local skill = skills[1]
      room:addTableMark(player, skillName, skill)
      room:handleAddLoseSkills(player, skill)
  
      if player:isAlive() then
        player:drawCards(1, skillName)
      end
    end,
  }
  
  -- 回合开始：可触发应有
  yingyou:addEffect(fk.TurnStart, yingyouSpec)
  
  -- 结束阶段触发已移除
  
  -- 受到伤害后：可触发谨有
  yingyou:addEffect(fk.Damaged, yingyouSpec)
  
  -- 额外结算与资源逻辑已移除
  
  return yingyou
  